package com.example.annolog.interceptor;

import com.example.annolog.anno.Log;
import com.example.annolog.domain.City;
import com.example.annolog.utils.ReflectUtils;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.concurrent.ThreadPoolExecutor;

@Component //step2-a:定义LogInterceptorAnno
public class LogInterceptorAnno implements HandlerInterceptor {


    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    final static Logger logger = LoggerFactory.getLogger(LogInterceptorAnno.class);

//    @Override
//    public Object invoke(MethodInvocation invocation) throws Throwable {
//
//
//        Log loganno = ReflectUtils.getAnnotationFromInvocation(invocation.getMethod(), Log.class);
//        Log logannoMethod = ReflectUtils.getMethodAnnotation(invocation.getMethod(), Log.class);
//        Log logannoClazz = ReflectUtils.getClassAnnotation(invocation.getMethod().getDeclaringClass(), Log.class);
//
//    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        City city=new City();
        // 如果不是映射到方法直接通过
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        // ①:START 方法注解级拦截器
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        // 判断接口是否需要登录
        Log logAnnotation = method.getAnnotation(Log.class);
        logger.info("--------------HttpServletRequest request----------------");
        logger.info("方法Header:" + request.getHeader("User-Agent"));
        logger.info("方法URI:" + request.getRequestURI());
        logger.info("方法ServletPath:" + request.getServletPath());
        logger.info("方法SessionId:" + request.getRequestedSessionId());
        logger.info("--------------Object handler------------------------");
        logger.info("方法DeclaringClass:" + method.getDeclaringClass());
        logger.info("方法methodAnnotation:" + method.getAnnotation(Log.class));
        logger.info("方法name:" + method.getName());
        logger.info("方法ParameterTypes:" + method.getParameterTypes());
        logger.info("注解的字段remark():" +logAnnotation.remark());


        // 有 @LoginRequired 注解，需要认证
        if (logAnnotation != null) {
            // 这写你拦截需要干的事儿，比如取缓存，SESSION，权限判断等
           // threadPoolTaskExecutor.execute(()->System.out.println("线程"));
            logger.info("===LogInterceptorAnno===TODO===preHandle===");
            return true;
        }
        return  true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.info("===LogInterceptorAnno===TODO===postHandle===");

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.info("===LogInterceptorAnno===TODO===afterCompletion===");

    }
}